package com.sea.base.widget

import android.widget.ImageView
import androidx.appcompat.widget.AppCompatImageView
import com.sea.base.adapter.holder.BaseViewHolder
import com.sea.base.adapter.simple.BaseAdapter
import com.sea.base.databinding.BaseDialogImgPreviewBinding
import com.sea.base.dialog.BaseViewBindingDialog
import com.sea.base.ext.view.MATCH_PARENT
import com.sea.base.ext.view.getDefLayoutParams
import com.sea.base.ext.view.loadUrl
import com.sea.base.ext.view.realContext
import com.sea.base.ext.view.setOnFastClickListener
import com.sea.base.ui.IUIContext

class ImgPreviewDialog(
    ui: com.sea.base.ui.IUIContext, private val imgList: List<String>,
    private val defPosition: Int,
) : BaseViewBindingDialog<BaseDialogImgPreviewBinding>(ui) {
    override fun onCreate() {
        vb.ivClose.setOnFastClickListener { dismiss() }
        vb.vpList.adapter = BaseAdapter.createList<String>(
            onCreateCallback = {
                BaseViewHolder(AppCompatImageView(it.realContext).apply {
                    scaleType = ImageView.ScaleType.FIT_CENTER
                    layoutParams = it.getDefLayoutParams().apply {
                        width = MATCH_PARENT
                        height = MATCH_PARENT
                    }
                })
            },
            onBindCallback = { holder, bean ->
                val iv = holder.itemView as ImageView
                iv.loadUrl(bean)
            })
            .apply {
                notifyDataSetChanged(imgList)
            }
        vb.vpList.setCurrentItem(defPosition, false)
    }
}